import Vue from 'vue'

import 'normalize.css/normalize.css' // A modern alternative to CSS resets

import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
// import 'element-ui/lib/theme-chalk/col.css'
import locale from 'element-ui/lib/locale/lang/en' // lang i18n zh-CN
// import i18n from './i18n'
import VueI18n from 'vue-i18n'
import 'element-ui/lib/theme-chalk/display.css'

import '@/styles/index.scss' // global css

import App from './App'
import store from './store'
import router from './router'

import '@/icons' // icon
import '@/permission' // permission control
import request from '@/utils/request'
import VueTimepicker from 'vue2-timepicker'
import 'vue2-timepicker/dist/VueTimepicker.css'
/**
 * If you don't want to use mock-server
 * you want to use MockJs for mock api
 * you can execute: mockXHR()
 *
 * Currently MockJs will be used in the production environment,
 * please remove it before going online! ! !
 */
import {
  mockXHR
} from '../mock'
import config from '../package.json'
Vue.prototype.version =  config.version
if (process.env.NODE_ENV === 'production') {
  mockXHR()
}

// 注册Print
import Print from 'vue-print-nb'
Vue.use(Print)

// 导入格式化时间的模块
import moment from 'moment'
console.log('撒大神啊', process)
Vue.prototype.$baseUrl = process.env.BASE_URL
Vue.prototype.$moment = moment
Vue.prototype.$ajax = request
  const dateFormat = function(dataStr, typ, typeStr = '-', cFormat) {
  const ts = new Date(dataStr)
  if (dataStr === '' || dataStr === undefined || ts == 'Invalid Date') {
    return '-'
  } else {
    var dt = new Date(dataStr)
    // yyyy-mm-dd

    var y = dt.getFullYear()
    var m = dt.toDateString().split(' ')[1] // dt.getMonth() + 1
    var d = dt.getDate()
    var h = dt.getHours()
    var i = dt.getMinutes()
    var s = dt.getSeconds()
    const formatObj = {
      d,
      m,
      y,
      h,
      i,
      s
    }
    const format = cFormat
    let time_str = ''
    if (format) {
      time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
        let value = formatObj[key]
        // Note: getDay() returns 0 on Sunday
        if (result.length > 0 && value < 10) {
          value = '0' + value
        }
        return value || ''
      })
      return time_str
    } else {
      return (typ === 'dmy' || !typ ? d + typeStr : '') + dt.toDateString().split(' ')[1] + typeStr + y + time_str
    }
  }
}
Vue.filter('dateFormat', dateFormat)
Vue.prototype.$dateFormat = dateFormat
// 全局注册 组件
const registerComponents = {
  install: (c) => {
    Vue.component('Pagination', () => import('@/components/Pagination'))
    Vue.component('EditDel', () => import('@/components/EditDel'))
    Vue.component('VueTimepicker', VueTimepicker)
  }
}
Vue.use(registerComponents)
Vue.prototype._buttonDisable = (that, buttonDisable, status) => {
  if (!status && !that[buttonDisable]) {
    var tS = setTimeout(() => {
      that[buttonDisable] = !that[buttonDisable]
      // debugger
    }, 5000)
    clearTimeout(tS)
  }
  that[buttonDisable] = !that[buttonDisable]
}
Vue.prototype.openLoading = (type, text) => {
  const loading = Vue.prototype.$loading()
  if (type) {
    Vue.prototype.$loading({
      lock: true,
      text: text || 'Loading',
      spinner: 'el-icon-loading',
      background: 'rgba(0, 0, 0, 0)'
    })
    setTimeout(function() {
      loading.close()
    }, 10000)
  } else {
    setTimeout(() => {
      loading.close()
    }, 500)
  }
}
Vue.prototype.getToDay = function(days) {
  // 昨天的时间
  var day1 = new Date()
  // day1.setTime(day1.getTime()-24*60*60*1000);
  // var s1 = day1.getFullYear()+"-" + (day1.getMonth()+1) + "-" + day1.getDate();
  // 今天的时间
  var day2 = new Date()
  var stFn = (d) => {
    return (d > 9 ? '' : '0') + d
  }

  day2.setTime(days || day2.getTime())
  var s2 = day2.getFullYear() + '-' + stFn(day2.getMonth() + 1) + '-' + stFn(day2.getDate())
  // 明天的时间
  // var day3 = new Date();
  // day3.setTime(day3.getTime()+24*60*60*1000);
  // var s3 = day3.getFullYear()+"-" + (day3.getMonth()+1) + "-" + day3.getDate();
  // 拼接时间
  // 赋值doubleDate
  return s2
}
const isNotStatus = function(list, that, _this) {
  if (!list) {
    return '-'
  } else {
    let strStatus = ''
    // console.log('沙雕', list, that, _this)
    strStatus = list.status === 'ACTIVE' ? 'Suspended' : 'Active'
    if (that) {
      // strStatus = list.status === 'ACTIVE' ? 'Suspended' : 'Active'
      that(list, 'status', strStatus.toUpperCase())
      that(list, '_status', strStatus)
    }
    return strStatus
  }
}

const clearInit = (row, $set, expKey) => {
  for (var o in row) {
    !row[o] && o !== expKey ? $set(row, o, '-') : ''
  }
  return ''
}
Vue.prototype.clearInit = clearInit
Vue.filter('clearInit', clearInit)
Vue.filter('isNotStatus', isNotStatus)
Vue.prototype.IsNotStatus = isNotStatus
Vue.filter('userStatusFormat', function(dataStr) {
  dataStr = parseInt(dataStr)
  if (dataStr === '') {
    return '-'
  } else {
    const array = ['Active', 'Suspended', 'Retired']
    return array[dataStr]
  }
})
Vue.filter('fixThTitle', (data, titleArr, r, indLis, $set) => {
  data.rateAndSport.map((lisd) => {
    if (lisd.sport === indLis.sport) {
      // debugger
      // lisd.rate = indLis.rate
     // return lisd
    } else {
      // console.log('data.rateAndSport',indLis.rate)
     // debugger
      // data.rateAndSport.push({sport:indLis.sport,rate:0})
      // return
    }
 //   console.log("data.rateAndSport")
  })
  // const rows = {
  //   sport: titleArr[r].sport,
  //   rate: titleArr[r].rate
  // }
  data.rateAndSport.push({ sport: indLis.sport, rate: 0 })
  $set(data, 'rateAndSport', data['rateAndSport'])
})

Vue.filter('categoryFormat', function(dataStr) {
  if (dataStr === '') {
    return '-'
  } else {
    if (dataStr === 'GENERAL_PRACTITIONER') return 'General Practitioner'
    if (dataStr === 'ORTHOPEDICE') return 'Orthopedics'
    if (dataStr === 'PROSTHETICS_ORTHOTICS') return 'Prosthetics & Orthotics'
    if (dataStr === 'PHYSIOTHERAPY') return 'Physiotherapy'
    if (dataStr === 'OPHTHALMOLOGY') return 'Ophthalmology'
    if (dataStr === 'OTHERS') return 'Others'

    if (dataStr === 'NUTRITION') return 'Nutrition'
    if (dataStr === 'PSYCHOLOGY') return 'Psychology'

    if (dataStr === 'DOPING') return 'Doping'
    if (dataStr === 'MEDICATION') return 'Medication'
    if (dataStr === 'TUE') return 'Tue'

    if (dataStr === 'PASSPORT') return 'Renewal' // 消息类型
    if (dataStr === 'HOME_PERMIT') return 'Renewal' // 消息类型
  }
})

Vue.prototype.$caseIdFormat = function(subscribeType, id) {
  if (subscribeType === 'TIMETABLE' || subscribeType === 'TimeTable') return 'MED-' + id
  if (subscribeType === 'COURSE' || subscribeType === 'Course') return ' SPS-' + id
  if (subscribeType === 'SPORT_MEDICINE' || subscribeType === 'SportMedicine') return 'MED-' + id
  if (subscribeType === 'SPORT_SCIENCE' || subscribeType === 'SportScience') return ' SPS-' + id
  if (subscribeType === 'CLASSIFICATION' || subscribeType === 'Classification') return 'CLA-' + id
  if (subscribeType === 'MEDICATION' || subscribeType === 'Medication') return 'DOP-' + id
  if (subscribeType === 'HOME_PERMIT') return 'RNW-' + id
  if (subscribeType === 'PASSPORT') return 'RNW-' + id
}

// 使用方法 let data = this.$getCode(this.password) 这样传给接口就行
import {
  JSEncrypt
} from 'jsencrypt'
Vue.prototype.$getCode = function(password) {
  const encrypt = new JSEncrypt()
  encrypt.setPublicKey(
    'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDHE6//+6MI+qzTKL/udfkypNtdxQlRO7Gmrp9XnWPI3DYu2ikAiL8KRCGGdHb2O5bc752Z1lK2e19dHSQFRISbJlmZVeC0aih3dfkmSEAwo+gZjwBy1MpabSiPYy6SKnALID5f9SFXghBcHQYWyn0ZTr4y8MfKT8xMRTm4S/zYYQIDAQAB'
  )
  const data = encrypt.encrypt(password)
  return data
}

Vue.prototype.$getUserStatus = function(status) {
  const array = ['Active', 'Suspended', 'Retired']
  return array[status]
}

Vue.prototype.$log = function(data) {
  var showLog = process.env.VUE_APP_ENV === 'development' ? true : ''
  if (showLog) data()
}

Vue.prototype.$uploadFormat = function(fileList) {
  const list = []
  for (var i in fileList) {
    if (fileList[i].status === 'success' && fileList[i].response) {
      list.push({
        name: fileList[i].name,
        url: fileList[i].response.result
      })
    } else {
      list.push({
        name: fileList[i].name,
        url: fileList[i].url
      })
    }
  }
  return list
}

const filterName = function(list, keyObj, str = ' ', addStr) {
  var TR = ''

  if (typeof (list) === 'object') {
    if (keyObj && typeof (keyObj) === 'object') {
      for (var i in keyObj) {
        TR += list[keyObj[i]] + (i < keyObj.length - 1 ? str : '')
      }
    } else if (list['lastName']) {
      TR = list['lastName'] + str + list['firstName'] + str + list['tcName']
    }
    if (addStr) TR += addStr + str
     //  console.log('dsfds', list,TR)
    return TR
  } else {
    return '-'
  }
}
Vue.prototype.FilterName = filterName
Vue.filter('FilterName', filterName)

// set ElementUI lang to EN
Vue.use(VueI18n)
Vue.use(ElementUI, {
  locale
})
Vue.config.lang = 'zh-cn'

// Vue.locale('en', e)
Vue.config.productionTip = false

new Vue({
  el: '#app',
  router,
  // i18n,
  store,
  render: h => h(App)
})
